package com.david.service;

import com.david.dao.ArticleDao;
import com.david.entity.Article;
import com.david.entity.Category;
import com.david.model.api.PageOf;
import com.david.utils.DateTimeUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class ArticleService {

    @Resource
    ArticleDao articleDao;

    public List<Article> findAll() {
        return articleDao.findAll();
    }

    public List<Article> GetArticleListByParentId(int parentId) {
        return articleDao.GetArticleListByParentId(parentId);
    }
    public void updateViewCount(int id){
         articleDao.updateViewCount(id);
    }
    public void downloadCount(int id){
        articleDao.downloadCount(id);
    }


    public List<Category> GetCategoryAndSelfByParentId(int parentId) {
        return articleDao.GetCategoryAndSelfByParentId(parentId);
    }

    public PageOf<Article> GetPageOfArticleByParentId(Integer parentId, String title, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageOf<Article> result = new PageOf<Article>();

        int count = articleDao.GetAllArticleByParentIdCount(parentId, title);

        PageHelper.startPage(page, pageSize);
        List<Article> dataList = articleDao.GetAllArticleByParentId(parentId, title);

        result.Data = dataList;
        result.Page = page;
        result.RowsCount = pageSize;
        result.TotalCount = count;
        result.TotalPages = (int) Math.ceil(result.TotalCount / pageSize);
        return result;
    }

    public List<Article> GetArticleListInParentId(int parentId) {
        return articleDao.GetArticleListInParentId(parentId);
    }


    public List<Article> findByParentId(Integer parentId) {
        return articleDao.findByParentId(parentId);
    }

    public Article GetArticleById(Integer id) {
        return articleDao.GetArticleById(id);
    }

    public int AddArticle(Article article) {
        if (article.createTime == null) {
            article.createTime = DateTimeUtils.GetPSTDateTime();
        }
        if (article.updateTime == null) {
            article.updateTime = DateTimeUtils.GetPSTDateTime();
        }
        article.isDelete = false;
        article.sort = 0;
        return articleDao.AddArticle(article);
    }

    public int updateArticle(Article article) {
        if (article.updateTime == null) {
            article.updateTime = DateTimeUtils.GetPSTDateTime();
        }
        return articleDao.updateArticle(article);
    }
    public int deleteArticle(Integer id){
        return articleDao.deleteArticle(id);
    }

    public Article Prev(Integer parentId, Integer id) {
        return articleDao.Prev(parentId, id);
    }

    public Article Next(Integer parentId, Integer id) {
        return articleDao.Next(parentId, id);
    }
}
